package svc.elib.linkpred;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;

import svc.elib.db.Author;
import svc.elib.socnet.Net;

public class AdamicAdar implements LPMetric {

	@Override
	public double score(Author a, Author b, Net n) {
		LinkedList<Author> cList = new LinkedList<Author>();
		
		Collection<Author> bNeis = n.getGraph().getNeighbors(b);
		Iterator<Author> anit = n.getGraph().getNeighbors(a).iterator();
		while (anit.hasNext()) {
			Author anei = anit.next();
			if (bNeis.contains(anei)) 
				cList.add(anei);
		}
		
		double sum = 0;
		for (int i = 0; i < cList.size(); i++) {
			int d = n.getGraph().degree(cList.get(i));
			sum += 1 / Math.log(d);
		}
		
		return sum;
	}

}
